package com.kzx.ticket.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kzx.ticket.model.MovieMsg;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kongz
 * @version 1.0 2021/11/16 14:33
 * @description 从艺恩获取票房数据
 */
public class TicketUtils {

    private static String url = "https://ys.endata.cn/enlib-api/api/movie/getMovie_BoxOffice_Year_List.do?" +
            "r=0.2774340720476598&datetype=Year" +
            "&date=2012-01-01,2021-12-31" +
            "&sdate=2012-01-01" +
            "&edate=2021-12-31" +
            "&dateid=2012,2021" +
            "&sdateid=2012" +
            "&edateid=2021" +
            "&bserviceprice=0" +
            "&columnslist=100,201,101,102,107,115,103,116,104,117,118,105,106" +
            "&pageindex=@" +
            "&pagesize=@" +
            "&order=102" +
            "&ordertype=desc";

    public static Map<String, Object> getParam(int pageIndex, int pageSize) {

        url = url.replaceFirst("@", String.valueOf(pageIndex)).replaceFirst("@", String.valueOf(pageSize));

        Map<String, Object> result = new HashMap<>();
        result.put("url", url);
        return result;
    }

    public static void main(String[] args) {
        Map<String, Object> map = HttpUtils.httpPost(TicketUtils.getParam(1, 16351));
        List<MovieMsg> list= new ArrayList<>();
        JSONObject jsonObject = JSONObject.parseObject(map.get("message").toString());
        JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("table1");
        for (int i = 0; i < jsonArray.size(); i++) {
            System.out.println("当前读取到第==>" + i);
            JSONObject json = jsonArray.getJSONObject(i);
            MovieMsg movieMsg = new MovieMsg();
            movieMsg.setIRank(json.getInteger("Irank") == null ? 0:json.getInteger("Irank"));
            movieMsg.setMovieID(json.getInteger("MovieID") == null ? 0:json.getInteger("MovieID"));
            movieMsg.setEntMovieID(json.getInteger("EntMovieID") == null ? 0:json.getInteger("EntMovieID"));
            movieMsg.setMovieName(json.getString("MovieName") == null ? "":json.getString("MovieName"));
            movieMsg.setEnMovieName(json.getString("EnMovieName") == null ? "":json.getString("EnMovieName"));
            movieMsg.setBoxOffice(json.getDouble("BoxOffice") == null ? 0.0:json.getDouble("BoxOffice"));
            movieMsg.setShowCount(json.getInteger("ShowCount") == null ? 0:json.getInteger("ShowCount"));
            movieMsg.setAudienceCount(json.getInteger("AudienceCount") == null ? 0:json.getInteger("AudienceCount"));
            movieMsg.setAvgBoxOffice(json.getInteger("AvgBoxOffice") == null ? 0:json.getInteger("AvgBoxOffice"));
            movieMsg.setAvgShowAudienceCount(json.getInteger("AvgShowAudienceCount") == null ? 0:json.getInteger("AvgShowAudienceCount"));
            movieMsg.setReleaseDate(json.getString("ReleaseDate") == null ? "":json.getString("ReleaseDate"));
            movieMsg.setTotalBoxOffice(json.getDouble("TotalBoxOffice") == null ? 0.0:json.getDouble("TotalBoxOffice"));
            movieMsg.setTotalShowCount(json.getInteger("TotalShowCount") == null ? 0:json.getInteger("TotalShowCount"));
            movieMsg.setTotalAudienceCount(json.getInteger("TotalAudienceCount") == null ? 0:json.getInteger("TotalAudienceCount"));
            movieMsg.setWeekBoxOffice(json.getDouble("WeekBoxOffice") == null ? 0.0:json.getDouble("WeekBoxOffice"));
            movieMsg.setYear(json.getInteger("Year"));
            list.add(movieMsg);
        }

        ExcelUtils.write("ticket.xlsx", "file/", list);
    }

}
